package com.htu.controller;

import com.htu.constant.CResultType;
import com.htu.domain.Result;
import com.htu.domain.User;
import com.htu.model.IUserModel;
import com.htu.service.SendMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class UserController
{
    @Autowired
    private IUserModel model;

    @PostMapping("/userLogin")
    public Result<User> doLogin(int id, String password)
    {
        User user = model.doLogin(id, password);
        int resultType = CResultType.FAIL;
        String msg = "fail";
        if (user != null)
        {
            resultType = CResultType.SUCCESS;
            msg = "success";
        }
        return new Result<User>(resultType, msg, user);
    }

    @PostMapping("/register")
    public Result<Boolean> doRegister(User user)
    {

        boolean registerState = model.doRegister(user);
        String msg = "fail";
        int resultType = CResultType.FAIL;
        if (registerState)
        {
            msg = "success";
            resultType = CResultType.SUCCESS;
        }

        return new Result<Boolean>(resultType, msg, registerState);
    }
    @PostMapping("/showAllPerson")
    public Result<List<User>> queryUserAll()
    {
        List<User> userMsg = model.queryUserAll();
        String msg = "fail";
        int resultType = CResultType.FAIL;
        if (userMsg.size() != 0)
        {
            msg = "success";
            resultType = CResultType.SUCCESS;
        }
        return new Result<List<User>>(resultType, msg, userMsg);
    }
    @PostMapping("/retrieveById")
    public Result<User> doLoginById(int id)
    {
        User user = model.doLoginById(id);
        int resultType = CResultType.FAIL;
        String msg = "fail";
        if (user != null)
        {
            resultType = CResultType.SUCCESS;
            msg = "success";
        }
        return new Result<User>(resultType, msg, user);
    }

    @PostMapping("/updateUser")
    public Result<Boolean> doUpdateUser(User user)
    {
        boolean updateState = model.doUpdateUser(user);
        String msg = "fail";
        int resultType = CResultType.FAIL;
        if (updateState)
        {
            msg = "success";
            resultType = CResultType.SUCCESS;
        }
        return new Result<Boolean>(resultType, msg, updateState);
    }

    @PostMapping("/update/password")
    public Result<Boolean> doUpdatePassword(int id, String password)
    {
        boolean updateState = model.doUpdatePassword(id, password);
        String msg = "fail";
        int resultType = CResultType.FAIL;
        if (updateState)
        {
            msg = "success";
            resultType = CResultType.SUCCESS;
        }

        return new Result<Boolean>(resultType, msg, updateState);
    }

    @PostMapping("/getEmailVerify")
    @ResponseBody

    public String getEmailVerify(String userEmail)
    {
        System.out.println("从前台接受的邮箱号：" + userEmail);
        if (userEmail != null)
        {
            //1.ver生成6位随机数
            int ver1 = (int) (Math.random() * 899999) + 100000;
            //2.类型转换
            String ver = String.valueOf(ver1);
            SendMessageService sendMessageService = new SendMessageService();
            //3.将HTML传来的值和生成的随机数传入发送邮件的工具类里
            sendMessageService.sendMessage(userEmail, ver1);
            //邮箱不为空将生成的随机验证码ver传回页面
            return ver;
        } else
        {
            //邮箱为空返回提示信息
            return "fail";
        }
    }


}
